<?php

namespace App\Http\Controllers\Customer;

use App\Models\Customer\Doctor;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use App\Helpers\Code;

class DoctorController extends Controller
{

    /**
     * DoctorController constructor.
     *
     * @author lql
     */
    public function __construct()
    {
        $this->model = new Doctor();

        $this->_validators();
    }

    /**
     * 添加客户医生表
     * @param Request $request
     * @return array
     *
     * @author lql
     */
    public function addDoctor(Request $request)
    {
        try {
            $data = $request->only(array_diff($this->model->getFillable(), ['code', 'created_by', 'updated_by', 'data_scope']));

            $validator = Validator::make($data, $this->rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->addDoctor($data);
            $this->checked($model, [
                'null' => 'customer.docker.customer_department_not_rely',
                Code::INSIDE_ERROR => 'customer.region_province_not_rely',
                Code::INSIDE_ERROR_1 => 'customer.type_not_exists',
                Code::INSIDE_ERROR_2 => 'customer.filed_not_exists',
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 编辑客户
     * @param Request $request
     * @param $id
     * @return array
     *
     * @author lql
     */
    public function editDoctor(Request $request, $id)
    {
        try {
            $data = $request->only(array_diff($this->model->getFillable(), ['created_by', 'updated_by', 'data_scope', 'code']));

            $rules = [];
            foreach ($data as $key => $value) {
                switch ($key) {
                    case 'intro':
                    case 'specialty':
                    case 'other':
                        break;
                    default:
                        $rules[$key] = $this->rules[$key];
                }
            }

            $validator = Validator::make($data, $rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->editDoctor($data, $id);
            $this->checked($model, [
                'null' => 'common.recored_not_found',
                Code::INSIDE_ERROR => 'customer.region_province_not_rely',
                Code::INSIDE_ERROR_1 => 'customer.docker.customer_department_not_rely',
                Code::INSIDE_ERROR_2 => 'customer.type_not_exists',
                Code::INSIDE_ERROR_3 => 'customer.filed_not_exists',
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取全部
     * @return array|void
     *
     * @author lql
     */
    public function getDoctorTree(Request $request)
    {
        try {
            $keyword = $request->input('keyword');

            $limit = (int)$request->input('limit');

            $sorter = $this->handleSort($request, $this->model->sorters);

            $defaultSorter = $request->input('sorter') ?? 'desc';

            return preprocess($this->model->getDoctorTree($limit, $sorter, $keyword, $defaultSorter));
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取医生详情
     * @param $id
     * @return array
     *
     * @author lql
     */
    public function getDoctor($id)
    {
        try {
            $model = $this->model->getDoctor($id);

            $this->checked($model, [
                'null' => 'common.recored_not_found',
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取医生列表
     * @param Request $request
     * @return array
     *
     * @author lql
     */
    public function getDoctorList(Request $request)
    {
        try {
            $search = $this->handleSearch($request, $this->model);

            $limit = (int)$request->input('limit');

            $sorter = $this->handleSort($request, $this->model->sorters);

            $defaultSorter = $request->input('sorter') ?? 'desc';

            return preprocess($this->model->getDoctorList($limit, $sorter, $search, $defaultSorter));
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function _validators()
    {
        $this->rules = [
            'customer_id' => 'bail|required',
            'department_id' => 'bail|required',
            'title' => 'bail|required|max:100',
            'job_title' => 'bail|nullable|max:200',
            'is_monthly' => 'bail|required|in:0,1,2',
            'salesman' => 'bail|nullable|exists:users,id',
            'phone' => 'bail|nullable|max:20',
            'email' => 'bail|nullable|max:100|email',
            'alipay' => 'bail|nullable|max:100',
            'wechat' => 'bail|nullable|max:100',
            'field' => 'bail|nullable|max:200',
            'type' => 'bail|required|max:200',
            'region' => 'bail|required',
            'province' => 'bail|required',
            'state' => 'bail|required|in:0,1',
        ];

        $this->messages = [
            'customer_id.required' => trans('customer.docker.customer_id_required'),
            'department_id.required' => trans('customer.docker.department_id_required'),
            'title.required' => trans('customer.title_required'),
            'title.max' => trans('customer.title_max'),
            'job_title.max' => trans('customer.docker.job_title_max'),
            'is_monthly.required' => trans('customer.docker.is_monthly_required'),
            'is_monthly.in' => trans('customer.docker.is_monthly_in'),
            'salesman.exists' => trans('customer.salesman_exists'),
            'phone.max' => trans('customer.phone_max'),
            'email.max' => trans('customer.email_max'),
            'email.email' => trans('customer.email_email'),
            'alipay.max' => trans('customer.docker.alipay_max'),
            'wechat.max' => trans('customer.docker.wechat_max'),
            'field.max' => trans('customer.docker.field_max'),
            'type.required' => trans('customer.docker.type_required'),
            'type.max' => trans('customer.docker.type_max'),
            'region.required' => trans('customer.region_required'),
            'province.required' => trans('customer.province_required'),
            'state.required' => trans('customer.state_required'),
            'state.in' => trans('customer.state_in'),
        ];
    }

}
